#include "stdafx.h"
#include "xControl.h"

xControl::xControl()
{
	m_pClassName = L"xControl";
	m_pRenderTarget = NULL;
	m_pBackBuffer = NULL;
	m_pBackBmp = NULL;
	m_pBgmBrush =
	m_pFrameBrush = NULL;
	m_bMouseEnter = FALSE;
	m_bRedraw = TRUE;
}

xControl::~xControl()
{	
	Release();
}

void xControl::Release()
{	
	COM_SAFE_RELEASE(m_pBgmBrush);	
	COM_SAFE_RELEASE(m_pFrameBrush);
	COM_SAFE_RELEASE(m_pBackBuffer);	
	COM_SAFE_RELEASE(m_pBackBmp);
}

void xControl::Create(const RECT &rect, ID2D1RenderTarget *pRenderTarget)
{
	ATLASSERT(m_pBackBuffer == NULL);

	m_rect = rect;
	m_pRenderTarget = pRenderTarget;

	if(pRenderTarget != NULL)
	{
		m_pRenderTarget->CreateCompatibleRenderTarget(D2D1::SizeF((FLOAT)rect.right - rect.left, (FLOAT)rect.bottom - rect.top), &m_pBackBuffer);
	}

	if(m_pBackBuffer != NULL)
	{
		m_pBackBuffer->SetAntialiasMode(D2D1_ANTIALIAS_MODE_ALIASED);
		m_pBackBuffer->GetBitmap(&m_pBackBmp);
		m_pBackBuffer->CreateSolidColorBrush(D2D1::ColorF(BGR(255, 255, 255), 0.5f), &m_pBgmBrush);
		m_pBackBuffer->CreateSolidColorBrush(D2D1::ColorF(BGR(255, 255, 255), 0.7f), &m_pFrameBrush);
	}
}

void xControl::OnRender()
{
	if(m_bRedraw)
	{
		m_bRedraw = FALSE;
		if(m_pBackBuffer != NULL)
		{
			m_pBackBuffer->BeginDraw();
			OnDraw();
			m_pBackBuffer->EndDraw();
		}
	}
	
	if(m_pBackBmp != NULL && m_pRenderTarget != NULL)
	{
		m_pRenderTarget->DrawBitmap(m_pBackBmp, D2D1::RectF((FLOAT)m_rect.left, (FLOAT)m_rect.top, (FLOAT)m_rect.right, (FLOAT)m_rect.bottom));
	}
}

void xControl::OnDraw()
{
	m_pBackBuffer->Clear(D2D1::ColorF(BGR(0, 0, 0), 0.0f));
	m_pBackBuffer->FillRectangle(D2D1::RectF(0.0f, 0.0f, (FLOAT)m_rect.right - m_rect.left, (FLOAT)m_rect.bottom - m_rect.top), m_pBgmBrush);
	m_pBackBuffer->DrawRectangle(D2D1::RectF(1.0f, 1.0f, (FLOAT)m_rect.right - m_rect.left, (FLOAT)m_rect.bottom - m_rect.top), m_pFrameBrush);
}

BOOL xControl::OnWndProc(UINT message, WPARAM wParam, LPARAM lParam)
{
	if(message == WM_LBUTTONDOWN)
	{
		POINT	point = {lParam & 0xFFFF, (lParam & 0xFFFF0000) >> 16};

		if(PtInRect(&m_rect, point))
		{
			OnLButtonDown(wParam, point);
			return TRUE;
		}
	}
	else if(message == WM_LBUTTONUP)
	{
		POINT	point = {lParam & 0xFFFF, (lParam & 0xFFFF0000) >> 16};

		if(PtInRect(&m_rect, point))
		{
			OnLButtonUp(wParam, point);
		}
	}
	else if(message == WM_MOUSEMOVE)
	{
		POINT	point = {lParam & 0xFFFF, (lParam & 0xFFFF0000) >> 16};

		if(PtInRect(&m_rect, point))
		{
			if(!m_bMouseEnter)
			{
				m_bMouseEnter = TRUE;
				OnMouseEnter();
			}
		}
		else
		{
			if(m_bMouseEnter)
			{
				m_bMouseEnter = FALSE;
				OnMouseLeave();
			}
		}
	}
	else if(message == WM_MOUSELEAVE)
	{
		if(m_bMouseEnter)
		{
			m_bMouseEnter = FALSE;
			OnMouseLeave();
		}
	}
	return FALSE;
}

void xControl::OnLButtonDown(UINT nFlags, POINT point)
{
}

void xControl::OnLButtonUp(UINT nFlags, POINT point)
{
}

void xControl::OnMouseEnter()
{
}

void xControl::OnMouseLeave()
{
}

void xControl::Delegate(DWORD dwEvent, xFrame *pFrame, FnEvent fnEvent)
{
}

void xControl::Reset(ID2D1RenderTarget *pRenderTarget, BOOL bReleaseOnly)
{
	Release();
	if(bReleaseOnly)
	{
		return ;
	}

	m_pRenderTarget = pRenderTarget;

	if(pRenderTarget != NULL)
	{
		m_pRenderTarget->CreateCompatibleRenderTarget(D2D1::SizeF((FLOAT)m_rect.right - m_rect.left, (FLOAT)m_rect.bottom - m_rect.top), &m_pBackBuffer);
	}

	if(m_pBackBuffer != NULL)
	{
		m_pBackBuffer->SetAntialiasMode(D2D1_ANTIALIAS_MODE_ALIASED);
		m_pBackBuffer->GetBitmap(&m_pBackBmp);
		m_pBackBuffer->CreateSolidColorBrush(D2D1::ColorF(BGR(255, 255, 255), 0.5f), &m_pBgmBrush);
		m_pBackBuffer->CreateSolidColorBrush(D2D1::ColorF(BGR(255, 255, 255), 0.7f), &m_pFrameBrush);
	}

	m_bRedraw = TRUE;
}